package com.css.dzj.dzjap.modules.apunit.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.HttpClientUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.apunit.entity.ApUnitEntity;
import com.css.dzj.dzjap.modules.apunit.service.ApunitService;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import com.querydsl.core.QueryResults;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 安评单位Controller
 * @author liyunyue
 */
@RestController
@RequestMapping("/apunit")
@PropertySource(value = "classpath:base.properties")
public class ApunitController {

    @Autowired
    private ApunitService apunitService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Value("${BASE_URL}")
    private  String BASE_URL;
    @Value("${CHECK_USER}")
    private String CHECK_USER;

    @Autowired
    BaseService baseService;

    /**
     * 安评单位注册
     * @param apUnitEntity
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody ApUnitEntity apUnitEntity) {
        try {
            if(apUnitEntity.getUniscid() == null) {
                return R.error("统一社会信用代码不能为空");
            }
            // 判断该单位是否在黑名单中
            ApEnterpriseCreditEntity tyReConEnterPrise = enterpriseService.getTyReConEnterPrise("0", apUnitEntity.getUniscid());
            if(tyReConEnterPrise != null) {
                return R.error("该安评单位已存在于黑名单中，无法注册!");
            }
            if(apUnitEntity.getUserName() == null) {
                return R.error("用户名不能为空");
            }
            // 判断统一社会信用代码是否已存在
            boolean isExistEntCode = apunitService.isExistEntCode(apUnitEntity.getUniscid());
            if(isExistEntCode) {
                return R.error("统一信用代码已存在!");
            }
            // 判断用户名是否已存在
            boolean isExistUserName = apunitService.isExistUserName(apUnitEntity.getUserName());
            if(isExistUserName) {
                return R.error("用户名已存在!");
            }
            /*String url=BASE_URL+CHECK_USER;
            String msg= HttpClientUtil.doGet(url+apUnitEntity.getUserName());*/
            boolean flag=baseService.checkUserExist(apUnitEntity.getUserName());
            if(flag) {
                return R.error("用户名已存在");
            }else {
                apunitService.regist(apUnitEntity);
                baseService.logSuccess(3,"添加单位成功","2");
                return R.ok();
            }

        }catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"添加单位失败","2");
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/querySpResult")
    public R queryResult(@RequestParam String mobile,
                         @RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<ApUnitEntity> apUnitEntityList = apunitService.queryByTel(pageRequest, mobile);
            baseService.logSuccess(3,"查询单位","2");
            return R.ok().put("data", apUnitEntityList);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"查询单位失败","2");
            return R.error(e.getMessage());
        }


    }


    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable String id) {
        ApUnitEntity apUnitEntity = apunitService.queryById(id);
        baseService.logSuccess(3,"查询单位","2");
        return R.ok().put("data", apUnitEntity);
    }
}
